home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / SpinButtonPanel.java < prev    next >
Text File  |  1998-08-21  |  15KB  |  547 lines

  1. package symantec.itools.awt.util.spinner;
  2.  
  3.  
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.beans.PropertyChangeListener;
  7. import java.beans.PropertyVetoException;
  8. import java.beans.VetoableChangeListener;
  9. import symantec.itools.awt.Orientation;
  10. import symantec.itools.awt.DirectionButton;
  11.  
  12.  
  13. //    05/30/97    RKM    Made it compile with some of the 1.1 component changes (added catches)
  14. //     06/02/97    MSH    Updated to Java 1.1 and "Good Bean" Spec
  15. //    06/03/97    LAB Polished update to Java 1.1.
  16. //                    Changed the package to symantec.itools.awt.util.spinner.
  17. //    07/11/97    LAB Updated calls to the deprecated methods enable() and disable() in
  18. //                    DirectionButton to the new call setEnabled(boolean).
  19. //                    Deprecated enable and disable functions, and replaced them with
  20. //                    setters and getters.
  21. //    07/18/97    LAB Added add/removeNotify to handle event listener registration.
  22. //  07/30/97    CAR inner adaptor class implements java.io.Serializable
  23. //                  implemented readObject
  24. //    08/28/97    LAB Changed the default look of the direction buttons to be more spinner oriented.
  25. //  02/05/98    DS  Re-write of GUI
  26.  
  27. /**
  28.  * This component provides the up/down or right/left buttons used in
  29.  * spinners. It is used by abstract class Spinner and in the HorizontalSpinButtonPanel
  30.  * and VerticalSpinButtonPanel components.
  31.  *
  32.  * @see symantec.itools.awt.Spinner
  33.  * @see symantec.itools.awt.HorizontalSpinButton
  34.  * @see symantec.itools.awt.VerticalSpinButton
  35.  * @version 1.1, Nov 26, 1996
  36.  * @author Symantec
  37.  */
  38. public class SpinButtonPanel
  39.     extends    Panel
  40.     implements Orientation
  41. {
  42.     /**
  43.      * This is the Adjustment Event handling innerclass.
  44.      */
  45.     class Action
  46.         implements ActionListener,
  47.                    java.io.Serializable
  48.     {
  49.         public void actionPerformed(ActionEvent e)
  50.         {
  51.             Object source;
  52.  
  53.             source = e.getSource();
  54.  
  55.             if(source == incBtn)
  56.             {
  57.                 sourceActionEvent("Increment");
  58.             }
  59.             else if(source == decBtn)
  60.             {
  61.                 sourceActionEvent("Decrement");
  62.             }
  63.         }
  64.     }
  65.  
  66.     /**
  67.      * Current spinner button layout.
  68.      * @see #getOrientation
  69.      * @see #setOrientation
  70.     */
  71.     protected int     orientation;
  72.     /**
  73.      * Whether the spinner buttons will continually post notify events
  74.      * while pressed.
  75.      * @see #getNotifyWhilePressed
  76.      * @see #setNotifyWhilePressed
  77.      */
  78.     protected boolean notifyWhilePressed;
  79.     /**
  80.      * Reserved.
  81.      */
  82.     protected int     delay;
  83.  
  84.     /**
  85.      * Constructs a SpinButtonPanel.
  86.      */
  87.     public SpinButtonPanel()
  88.     {
  89.         //{{INIT_CONTROLS
  90.         super.setLayout(new GridLayout(2,1,0,0));
  91.         setSize(104,51);
  92.         incBtn = new symantec.itools.awt.DirectionButton();
  93.  
  94.         try
  95.         {
  96.             incBtn.setDirection(symantec.itools.awt.DirectionButton.UP);
  97.         }
  98.         catch(java.beans.PropertyVetoException e) { }
  99.         incBtn.setBounds(0,0,104,25);
  100.         add(incBtn);
  101.         decBtn = new symantec.itools.awt.DirectionButton();
  102.         try {
  103.             decBtn.setDirection(symantec.itools.awt.DirectionButton.DOWN);
  104.         }
  105.         catch(java.beans.PropertyVetoException e) { }
  106.         decBtn.setBounds(0,25,104,25);
  107.         add(decBtn);
  108.         //}}
  109.     }
  110.  
  111.     //{{DECLARE_CONTROLS
  112.     symantec.itools.awt.DirectionButton incBtn;
  113.     symantec.itools.awt.DirectionButton decBtn;
  114.     //}}
  115.  
  116.     /**
  117.      * Sets whether the spinner buttons are laid out one above the other or
  118.      * one beside the other.
  119.      * @param o the new orientation, one of: ORIENTATION_VERTICAL, or ORIENTATION_HORIZONTAL
  120.      * @see #getOrientation
  121.      * @see symantec.itools.awt.Orientation#ORIENTATION_VERTICAL
  122.      * @see symantec.itools.awt.Orientation#ORIENTATION_HORIZONTAL
  123.     */
  124.     public void setOrientation(int o)
  125.     {
  126.         if(o != orientation)
  127.         {
  128.             orientation = o;
  129.  
  130.             switch(orientation)
  131.             {
  132.                 case ORIENTATION_VERTICAL :
  133.                 {
  134.                     super.setLayout(new GridLayout(2,1,0,0));
  135.                     break;
  136.                 }
  137.                 case ORIENTATION_HORIZONTAL :
  138.                 {
  139.                     super.setLayout(new GridLayout(1,2,0,0));
  140.                     break;
  141.                 }
  142.             }
  143.  
  144.             invalidate();
  145.             validate();
  146.         }
  147.     }
  148.  
  149.     /**
  150.      * Gets whether the spinner buttons are laid out one above the other or
  151.      * one beside the other.
  152.      * @return the orientation, one of: ORIENTATION_VERTICAL, or ORIENTATION_HORIZONTAL
  153.      * @see #setOrientation
  154.      * @see symantec.itools.awt.Orientation#ORIENTATION_VERTICAL
  155.      * @see symantec.itools.awt.Orientation#ORIENTATION_HORIZONTAL
  156.     */
  157.     public int getOrientation()
  158.     {
  159.         return (orientation);
  160.     }
  161.  
  162.     /**
  163.      * Sets whether the spinner buttons will continually post notify events
  164.      * while pressed.
  165.      * @param f true = send messages; false = do not send messages
  166.      * @see #isNotifyWhilePressed
  167.      * @see #setDelay
  168.      * @see #getDelay
  169.      *
  170.      * @exception PropertyVetoException
  171.      * if the specified property value is unacceptable
  172.      */
  173.     public void setNotifyWhilePressed(boolean f)
  174.         throws PropertyVetoException
  175.     {
  176.         incBtn.setNotifyWhilePressed(f);
  177.         decBtn.setNotifyWhilePressed(f);
  178.     }
  179.  
  180.     /**
  181.      * Gets whether the spinner buttons will continually post notify events
  182.      * while pressed.
  183.      * @return true if notify events posted while pressed, false otherwise
  184.      * @see #setNotifyWhilePressed
  185.      * @see #setDelay
  186.      * @see #getDelay
  187.      */
  188.     public boolean isNotifyWhilePressed()
  189.     {
  190.         return incBtn.isNotifyWhilePressed();
  191.     }
  192.  
  193.     /**
  194.      * @deprecated
  195.      * @see #isNotifyWhilePressed
  196.      */
  197.     public boolean getNotifyWhilePressed()
  198.     {
  199.         return isNotifyWhilePressed();
  200.     }
  201.  
  202.     /**
  203.      * Sets the notification event delay of the spinner buttons in milliseconds.
  204.      * @param d the delay between notification events in milliseconds
  205.      * @see #setNotifyWhilePressed
  206.      * @see #getDelay
  207.      *
  208.      * @exception PropertyVetoException
  209.      * if the specified property value is unacceptable
  210.      */
  211.     public void setDelay(int d)
  212.         throws PropertyVetoException
  213.     {
  214.         incBtn.setNotifyDelay(d);
  215.         decBtn.setNotifyDelay(d);
  216.     }
  217.  
  218.     /**
  219.      * Returns the current delay between notification events of the spinner
  220.      * buttons in milliseconds.
  221.      * @see #setNotifyWhilePressed
  222.      * @see #setDelay
  223.      */
  224.     public int getDelay()
  225.     {
  226.         return incBtn.getNotifyDelay();
  227.     }
  228.  
  229.     /**
  230.      * Takes no action.
  231.      * This is a standard Java AWT method which gets called to specify
  232.      * which layout manager should be used to layout the components in
  233.      * standard containers.
  234.      *
  235.      * Since layout managers CANNOT BE USED with this container the standard
  236.      * setLayout has been OVERRIDDEN for this container and does nothing.
  237.      *
  238.      * @param l the layout manager to use to layout this container's components
  239.      * (IGNORED)
  240.      * @see java.awt.Container#getLayout
  241.      **/
  242.     public void setLayout(LayoutManager l)
  243.     {
  244.     }
  245.  
  246.     /**
  247.      * Enables or disables this component so that it will respond to user input
  248.      * or not.
  249.      * This is a standard Java AWT method which gets called to enable or disable
  250.      * this component. Once enabled this component will respond to user input.
  251.      * @param flag true if the component is to be enabled,
  252.      * false if it is to be disabled.
  253.      *
  254.      * @see java.awt.Component#isEnabled
  255.      */
  256.     public synchronized void setEnabled(boolean flag)
  257.     {
  258.         if(isEnabled() != flag)
  259.         {
  260.             if(flag)
  261.             {
  262.                 super.enable();
  263.                 incBtn.setEnabled(true);
  264.                 decBtn.setEnabled(true);
  265.             }
  266.             else
  267.             {
  268.                 super.disable();
  269.                 incBtn.setEnabled(false);
  270.                 decBtn.setEnabled(false);
  271.             }
  272.         }
  273.     }
  274.  
  275.     /**
  276.      * @deprecated
  277.      * @see #setEnabled
  278.      */
  279.     public synchronized void enable()
  280.     {
  281.         setEnabled(true);
  282.     }
  283.  
  284.     /**
  285.      * @deprecated
  286.      * @see #setEnabled
  287.      */
  288.     public synchronized void disable()
  289.     {
  290.         setEnabled(false);
  291.     }
  292.  
  293.     /**
  294.      * This enables or disables the incrementing button only.
  295.      * @param flag true if the incrementing button is to be enabled,
  296.      * false if it is to be disabled.
  297.      * @see #isUpButtonEnabled
  298.      */
  299.     public synchronized void setUpButtonEnabled(boolean flag)
  300.     {
  301.         if(isUpButtonEnabled() != flag)
  302.         {
  303.             if(flag)
  304.             {
  305.                 incBtn.setEnabled(true);
  306.             }
  307.             else
  308.             {
  309.                 incBtn.setEnabled(false);
  310.             }
  311.         }
  312.     }
  313.  
  314.     /**
  315.      * The enabled state of the incrementing button.
  316.      * @return true if the incrementing button is enabled,
  317.      * false if it is disabled.
  318.      * @see #setUpButtonEnabled
  319.      */
  320.     public boolean isUpButtonEnabled()
  321.     {
  322.         return incBtn.isEnabled();
  323.     }
  324.  
  325.     /**
  326.      * This enables or disables the decrementing button only.
  327.      * @param flag true if the decrementing button is to be enabled,
  328.      * false if it is to be disabled.
  329.      * @see #isDownButtonEnabled
  330.      */
  331.     public synchronized void setDownButtonEnabled(boolean flag)
  332.     {
  333.         if(isDownButtonEnabled() != flag)
  334.         {
  335.             if(flag)
  336.             {
  337.                 decBtn.setEnabled(true);
  338.             }
  339.             else
  340.             {
  341.                 decBtn.setEnabled(false);
  342.             }
  343.         }
  344.     }
  345.  
  346.     /**
  347.      * The enabled state of the decrementing button.
  348.      * @return true if the decrementing button is enabled,
  349.      * false if it is disabled.
  350.      * @see #setDownButtonEnabled
  351.      */
  352.     public boolean isDownButtonEnabled()
  353.     {
  354.         return decBtn.isEnabled();
  355.     }
  356.  
  357.     /**
  358.      * @deprecated
  359.      * @see #setUpButtonEnabled
  360.      */
  361.     public synchronized void enableUpButton()
  362.     {
  363.         setUpButtonEnabled(true);
  364.     }
  365.  
  366.     /**
  367.      * @deprecated
  368.      * @see #setDownButtonEnabled
  369.      */
  370.     public synchronized void enableDownButton()
  371.     {
  372.         setDownButtonEnabled(true);
  373.     }
  374.  
  375.     /**
  376.      * @deprecated
  377.      * @see #setUpButtonEnabled
  378.      */
  379.     public synchronized void disableUpButton()
  380.     {
  381.         setUpButtonEnabled(false);
  382.     }
  383.  
  384.     /**
  385.      * @deprecated
  386.      * @see #setDownButtonEnabled
  387.      */
  388.     public synchronized void disableDownButton()
  389.     {
  390.         setDownButtonEnabled(false);
  391.     }
  392.  
  393.     /**
  394.      * Tells this component that it has been added to a container.
  395.      * This is a standard Java AWT method which gets called by the AWT when
  396.      * this component is added to a container. Typically, it is used to
  397.      * create this component's peer.
  398.      *
  399.      * It has been overridden here to hook-up event listeners.
  400.      *
  401.      * @see #removeNotify
  402.      */
  403.     public synchronized void addNotify()
  404.     {
  405.         super.addNotify();
  406.  
  407.         //Hook up listeners
  408.         if(action == null)
  409.         {
  410.             action = new Action();
  411.             incBtn.addActionListener(action);
  412.             decBtn.addActionListener(action);
  413.         }
  414.     }
  415.  
  416.     /**
  417.      * Tells this component that it is being removed from a container.
  418.      * This is a standard Java AWT method which gets called by the AWT when
  419.      * this component is removed from a container. Typically, it is used to
  420.      * destroy the peers of this component and all its subcomponents.
  421.      *
  422.      * It has been overridden here to unhook event listeners.
  423.      *
  424.      * @see #addNotify
  425.      */
  426.     public synchronized void removeNotify()
  427.     {
  428.         //Unhook listeners
  429.         if(action != null)
  430.         {
  431.             incBtn.removeActionListener(action);
  432.             decBtn.removeActionListener(action);
  433.             action = null;
  434.         }
  435.  
  436.         super.removeNotify();
  437.     }
  438.  
  439.     /**
  440.      * Adds a listener for all property change events.
  441.      * @param listener the listener to add
  442.      * @see #removePropertyChangeListener
  443.      */
  444.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  445.     {
  446.         changes.addPropertyChangeListener(listener);
  447.     }
  448.  
  449.     /**
  450.      * Removes a listener for all property change events.
  451.      * @param listener the listener to remove
  452.      * @see #addPropertyChangeListener
  453.      */
  454.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  455.     {
  456.         changes.removePropertyChangeListener(listener);
  457.     }
  458.  
  459.     /**
  460.      * Adds a listener for all vetoable property change events.
  461.      * @param listener the listener to add
  462.      * @see #removeVetoableChangeListener
  463.      */
  464.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  465.     {
  466.         vetos.addVetoableChangeListener(listener);
  467.     }
  468.  
  469.     /**
  470.      * Removes a listener for all vetoable property change events.
  471.      * @param listener the listener to remove
  472.      * @see #addVetoableChangeListener
  473.      */
  474.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  475.     {
  476.         vetos.removeVetoableChangeListener(listener);
  477.     }
  478.  
  479.     /**
  480.      * Adds the specified action listener to receive action events.
  481.      * The ActionCommand will be either "Increment" or "Decrement"
  482.      * depending on which spinner button was pressed.
  483.      * @param l the action listener
  484.      */
  485.     public synchronized void addActionListener(ActionListener l)
  486.     {
  487.         actionListener = AWTEventMulticaster.add(actionListener, l);
  488.     }
  489.  
  490.     /**
  491.      * Removes the specified action listener so it no longer receives
  492.      * action events from this component.
  493.      * @param l the action listener
  494.      */
  495.     public synchronized void removeActionListener(ActionListener l)
  496.     {
  497.         actionListener = AWTEventMulticaster.remove(actionListener, l);
  498.     }
  499.  
  500.     public Dimension getPreferredSize()
  501.     {
  502.         int h;
  503.         int w;
  504.  
  505.         h = 0;
  506.         w = 0;
  507.  
  508.         switch(orientation)
  509.         {
  510.             case ORIENTATION_VERTICAL :
  511.             {
  512.                 w = Math.max(incBtn.getPreferredSize().width, decBtn.getPreferredSize().width);
  513.                 h = incBtn.getPreferredSize().height + decBtn.getPreferredSize().height;
  514.                 break;
  515.             }
  516.             case ORIENTATION_HORIZONTAL :
  517.             {
  518.                 w = incBtn.getPreferredSize().width + decBtn.getPreferredSize().width;
  519.                 h = Math.max(incBtn.getPreferredSize().height, decBtn.getPreferredSize().height);
  520.                 break;
  521.             }
  522.         }
  523.  
  524.         return (new Dimension(w, h));
  525.     }
  526.  
  527.     /**
  528.      * Fire an action event to the listeners
  529.      */
  530.     protected void sourceActionEvent(String actionCommand)
  531.     {
  532.         if(actionListener != null)
  533.         {
  534.             actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
  535.         }
  536.     }
  537.  
  538.     /**
  539.      * The action listener to keep track of listeners for our action event.
  540.      */
  541.     protected ActionListener actionListener = null;
  542.  
  543.     private Action action = null;
  544.     private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  545.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  546. }
  547.